package com.hmall.item.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.item.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import java.io.IOException;
import java.util.Collections;

@RequiredArgsConstructor
public class ItemListener {
    private final ItemClient itemClient;
    private final RestHighLevelClient restHighLevelClient=new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.100.101:9200")
    ));
    /**
     * 监听新增商品
     */
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="item.index.save.queue", durable="true"),
            exchange = @Exchange(name="item.direct", type= ExchangeTypes.DIRECT),
            key={"item.index.save"}
    ))
    public void saveItem(Long id) throws IOException {
        //读取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr= JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        IndexRequest request = new IndexRequest("items").id(itemDTO.getId().toString());
        //准备参数
        request.source(jsonStr, XContentType.JSON);
        //发送请求
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println("保存成功");
    }
    //监听商品更新状态  局部更新
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="item.index.updateStatus.queue", durable="true"),
            exchange = @Exchange(name="item.direct", type= ExchangeTypes.DIRECT),
            key={"item.updateStatus"}
    ))
    public void updateStatus(Long id) throws IOException {
        //读取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr= JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        IndexRequest request = new IndexRequest("items").id(itemDTO.getId().toString());
        //准备参数
        request.source(jsonStr, XContentType.JSON);
        //发送请求
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println("更新成功");
    }
    //监听更新商品  全局更新
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="item.index.update.queue", durable="true"),
            exchange = @Exchange(name="item.direct", type= ExchangeTypes.DIRECT),
            key={"item.index.update"}
    ))
    public void updateItem( Long id) throws IOException {
        //读取数据库对象
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO==null){
            return;
        }
        //转换
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //json化
        String jsonStr= JSONUtil.toJsonStr(itemDoc);
        //准备request对象
        IndexRequest request = new IndexRequest("items").id(itemDTO.getId().toString());
        //准备参数
        request.source(jsonStr, XContentType.JSON);
        //发送请求
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println("更新成功");
    }
    //监听删除商品
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="item.index.delete.queue", durable="true"),
            exchange = @Exchange(name="item.direct", type= ExchangeTypes.DIRECT),
            key={"item.index.delete"}
    ))
    public void deleteItem( Long id) throws IOException {
        //准备request对象
        DeleteRequest request = new DeleteRequest("items").id(id.toString());
        //发送请求
        restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        System.out.println("删除成功");
    }



}
